﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	internal class CmdTools
	{
		public static void cdSetSpaceValue(ExternalCommandData revit)
		{
			XmlNodeList spaceTypeValue = XmlInfo.GetSpaceTypeValue("默认");
			RevitTools.SetSpaceData(revit, spaceTypeValue);
		}

		public static XmlNode cdGetCurrSel(ExternalCommandData revit, string strCurrentSel)
		{
			if (strCurrentSel != "")
			{
				if (XmlInfo.hyXmlDoc.InnerXml == "")
				{
					XmlInfo.hyLoadXml();
				}
				return XmlInfo.hyXmlDoc.SelectSingleNode("SelSets/SelSet[@Text='" + strCurrentSel + "']");
			}
			return null;
		}

		public static ElementSet cdGetCateElment(ExternalCommandData commandData, XmlNodeList nodeList, string selectName)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			ElementSet elementSet = new ElementSet();
			foreach (object obj in nodeList)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.InnerText == "风管")
				{
					IEnumerator enumerator2 = RevitTools.hyGetDuct(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj2 = enumerator2.Current;
							Element element = (Element)obj2;
							elementSet.Insert(element);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "风管管件")
				{
					IEnumerator enumerator2 = RevitTools.hyGetDuctFitting(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj3 = enumerator2.Current;
							Element element2 = (Element)obj3;
							elementSet.Insert(element2);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "风管阀件")
				{
					IEnumerator enumerator2 = RevitTools.hyGetDuctAccessory(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj4 = enumerator2.Current;
							Element element3 = (Element)obj4;
							elementSet.Insert(element3);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "风口")
				{
					foreach (object obj5 in RevitTools.hyGetDuctTerminal(activeUIDocument, selectName))
					{
						Element element4 = (Element)obj5;
						elementSet.Insert(element4);
					}
					IEnumerator enumerator2 = RevitTools.hyGetDuctTerminalTag(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj6 = enumerator2.Current;
							Element element5 = (Element)obj6;
							elementSet.Insert(element5);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "水管")
				{
					IEnumerator enumerator2 = RevitTools.hyGetPipe(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj7 = enumerator2.Current;
							Element element6 = (Element)obj7;
							elementSet.Insert(element6);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "水管管件")
				{
					IEnumerator enumerator2 = RevitTools.hyGetPipeFitting(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj8 = enumerator2.Current;
							Element element7 = (Element)obj8;
							elementSet.Insert(element7);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "水管阀件")
				{
					IEnumerator enumerator2 = RevitTools.hyGetPipeAccessory(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj9 = enumerator2.Current;
							Element element8 = (Element)obj9;
							elementSet.Insert(element8);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "机械设备")
				{
					IEnumerator enumerator2 = RevitTools.hyGetMech(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj10 = enumerator2.Current;
							Element element9 = (Element)obj10;
							elementSet.Insert(element9);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "风机")
				{
					IEnumerator enumerator2 = RevitTools.hyGetFengJi(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj11 = enumerator2.Current;
							Element element10 = (Element)obj11;
							elementSet.Insert(element10);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "盘管")
				{
					IEnumerator enumerator2 = RevitTools.hyGetPanGuan(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj12 = enumerator2.Current;
							Element element11 = (Element)obj12;
							elementSet.Insert(element11);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "墙")
				{
					IEnumerator enumerator2 = RevitTools.hyGetWall(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj13 = enumerator2.Current;
							Element element12 = (Element)obj13;
							elementSet.Insert(element12);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "门")
				{
					IEnumerator enumerator2 = RevitTools.hyGetDoor(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj14 = enumerator2.Current;
							Element element13 = (Element)obj14;
							elementSet.Insert(element13);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "窗")
				{
					IEnumerator enumerator2 = RevitTools.hyGetWindows(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj15 = enumerator2.Current;
							Element element14 = (Element)obj15;
							elementSet.Insert(element14);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "楼板")
				{
					IEnumerator enumerator2 = RevitTools.hyGetFloors(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj16 = enumerator2.Current;
							Element element15 = (Element)obj16;
							elementSet.Insert(element15);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "天花板")
				{
					IEnumerator enumerator2 = RevitTools.hyGetCeilings(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj17 = enumerator2.Current;
							Element element16 = (Element)obj17;
							elementSet.Insert(element16);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "电缆桥架")
				{
					IEnumerator enumerator2 = RevitTools.hyGetCableTray(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj18 = enumerator2.Current;
							Element element17 = (Element)obj18;
							elementSet.Insert(element17);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "导线")
				{
					IEnumerator enumerator2 = RevitTools.hyGetWire(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj19 = enumerator2.Current;
							Element element18 = (Element)obj19;
							elementSet.Insert(element18);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "线管")
				{
					IEnumerator enumerator2 = RevitTools.hyGetConduit(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj20 = enumerator2.Current;
							Element element19 = (Element)obj20;
							elementSet.Insert(element19);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "电缆桥架配件")
				{
					IEnumerator enumerator2 = RevitTools.hyGetCableTrayFitting(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj21 = enumerator2.Current;
							Element element20 = (Element)obj21;
							elementSet.Insert(element20);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "线管配件")
				{
					IEnumerator enumerator2 = RevitTools.hyGetConduitFitting(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj22 = enumerator2.Current;
							Element element21 = (Element)obj22;
							elementSet.Insert(element21);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "柱")
				{
					IEnumerator enumerator2 = RevitTools.hyGetColumns(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj23 = enumerator2.Current;
							Element element22 = (Element)obj23;
							elementSet.Insert(element22);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "灯具")
				{
					IEnumerator enumerator2 = RevitTools.hyGetLightingFixtures(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj24 = enumerator2.Current;
							Element element23 = (Element)obj24;
							elementSet.Insert(element23);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "开关")
				{
					IEnumerator enumerator2 = RevitTools.hyGetLightingDevices(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj25 = enumerator2.Current;
							Element element24 = (Element)obj25;
							elementSet.Insert(element24);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "插座")
				{
					IEnumerator enumerator2 = RevitTools.hyGetElectricalFixtures(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj26 = enumerator2.Current;
							Element element25 = (Element)obj26;
							elementSet.Insert(element25);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "动力")
				{
					IEnumerator enumerator2 = RevitTools.hyGetDongli(activeUIDocument, selectName).GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj27 = enumerator2.Current;
							Element element26 = (Element)obj27;
							elementSet.Insert(element26);
						}
						continue;
					}
				}
				if (xmlNode.InnerText == "箱柜")
				{
					foreach (object obj28 in RevitTools.hyGetXiangGui(activeUIDocument, selectName))
					{
						Element element27 = (Element)obj28;
						elementSet.Insert(element27);
					}
				}
			}
			return elementSet;
		}

		public static void ThreadMethodSel3D()
		{
			UIDocument activeUIDocument = CmdTools.cmdDataThread.Application.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application application = CmdTools.cmdDataThread.Application.Application;
			Transaction transaction = new Transaction(activeUIDocument.Document, "FilterSelect");
			try
			{
				transaction.Start();
				View3D activeView = activeUIDocument.Document.CreatHYView3D(new XYZ(-1.0, 1.0, -1.0));
				activeUIDocument.ActiveView = activeView;
			}
			catch (Exception ex)
			{
				ex.ToString();
				transaction.RollBack();
				return;
			}
		}

		public static Result cdSel3D(ExternalCommandData commandData, string selectName)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application application = commandData.Application.Application;
			Transaction transaction = new Transaction(activeUIDocument.Document, "FilterSelect");
            Autodesk.Revit.DB.View view3D = XmlInfo.View3D;
			Thread thread = null;
			try
			{
				transaction.Start();
				thread = WinFormTools.ThreadShowProgressBar(commandData);
				if (thread != null)
				{
					thread.Start();
				}
				XmlInfo.View3D = activeUIDocument.Document.CreatHYView3D(new XYZ(-1.0, 1.0, -1.0));
				ElementSet elementSet = RevitTools.hyGetAllElement(commandData);
				if (elementSet.Size == 0)
				{
					TaskDialog.Show("盈建科提示", "元素为空");
					transaction.Commit();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				XmlNodeList nodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet[@Text='" + selectName + "']/Cates/Cate");
				ElementSet elementSet2 = CmdTools.cdGetCateElment(commandData, nodeList, selectName);
				if (elementSet2.Size == 0)
				{
					TaskDialog.Show("盈建科提示", "元素为空");
					transaction.Commit();
					WinFormTools.DisThread(thread);
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				WinFormTools.MsgBox(elementSet2.Size.ToString());
				XmlInfo.View3D.HideYJKElements(elementSet);
				XmlInfo.View3D.UnHideYJKElements(elementSet2);
				transaction.Commit();
				WinFormTools.DisThread(thread);
				activeUIDocument.ActiveView = XmlInfo.View3D;
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				WinFormTools.DisThread(thread);
				WinFormTools.MsgBox(ex.ToString());
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result cdSelect(ExternalCommandData commandData, string selectName, bool isSelect)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Application.ActiveUIDocument.Selection;
			XmlNodeList nodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet[@Text='" + selectName + "']/Cates/Cate");
			Transaction transaction = new Transaction(activeUIDocument.Document, "www.hongye.com.cn");
			Thread thread = null;
			try
			{
				transaction.Start();
				thread = WinFormTools.ThreadShowProgressBar(commandData);
				if (thread != null)
				{
					thread.Start();
				}
				ElementSet elementSet = CmdTools.cdGetCateElment(commandData, nodeList, selectName);
				List<Element> list = new List<Element>();
				foreach (object obj in elementSet)
				{
					Element item = (Element)obj;
					if (isSelect)
					{
						list.Add(item);
					}
				}
				RevitVersionFuncs.SetSelection(selection, list);
				transaction.Commit();
				WinFormTools.DisThread(thread);
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				WinFormTools.DisThread(thread);
				WinFormTools.MsgBox(ex.ToString());
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result cdLock(ExternalCommandData commandData, string selectName, bool isLock)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			XmlNodeList nodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet[@Text='" + selectName + "']/Cates/Cate");
			Transaction transaction = new Transaction(activeUIDocument.Document, "www.hongye.com.cn");
			Thread thread = null;
			try
			{
				transaction.Start();
				thread = WinFormTools.ThreadShowProgressBar(commandData);
				if (thread != null)
				{
					thread.Start();
				}
				RevitVersionFuncs.ClearSelection(selection);
				ElementSet elementSet = CmdTools.cdGetCateElment(commandData, nodeList, selectName);
				List<Element> list = new List<Element>();
				foreach (object obj in elementSet)
				{
					Element element = (Element)obj;
					list.Add(element);
					if (isLock)
					{
						element.Pinned = true;
					}
					else
					{
						element.Pinned = false;
					}
				}
				RevitVersionFuncs.SetSelection(selection, list);
				transaction.Commit();
				WinFormTools.DisThread(thread);
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				WinFormTools.DisThread(thread);
				WinFormTools.MsgBox(ex.ToString());
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result cdLockOther(ExternalCommandData commandData, string selectName, bool isLock)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			XmlNodeList nodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet[@Text='" + selectName + "']/Cates/Cate");
			Transaction transaction = new Transaction(activeUIDocument.Document, "www.hongye.com.cn");
			Thread thread = null;
			try
			{
				transaction.Start();
				thread = WinFormTools.ThreadShowProgressBar(commandData);
				if (thread != null)
				{
					thread.Start();
				}
				ElementSet elementSet = CmdTools.cdGetCateElment(commandData, nodeList, selectName);
				ElementSet elementSet2 = RevitTools.hyGetAllElement(commandData);
				foreach (object obj in elementSet)
				{
					Element element = (Element)obj;
					elementSet2.Erase(element);
				}
				if (elementSet2.Size == 0)
				{
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				foreach (object obj2 in elementSet2)
				{
					Element element2 = (Element)obj2;
					if (isLock)
					{
						element2.Pinned = true;
					}
					else
					{
						element2.Pinned = false;
					}
				}
				transaction.Commit();
				WinFormTools.DisThread(thread);
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				WinFormTools.DisThread(thread);
				WinFormTools.MsgBox(ex.ToString());
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result cdHide(ExternalCommandData commandData, string selectName, bool isShow)
		{
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			XmlNodeList nodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet[@Text='" + selectName + "']/Cates/Cate");
			Transaction transaction = new Transaction(activeUIDocument.Document, "www.hongye.com.cn");
			Thread thread = null;
			try
			{
				transaction.Start();
				thread = WinFormTools.ThreadShowProgressBar(commandData);
				if (thread != null)
				{
					thread.Start();
				}
				ElementSet elementSet = CmdTools.cdGetCateElment(commandData, nodeList, selectName);
				if (elementSet.Size == 0)
				{
					WinFormTools.MsgBox("元素为空！");
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				if (isShow)
				{
					application.ActiveUIDocument.ActiveView.UnHideYJKElements(elementSet);
				}
				else
				{
					application.ActiveUIDocument.ActiveView.HideYJKElements(elementSet);
				}
				transaction.Commit();
				WinFormTools.DisThread(thread);
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				WinFormTools.DisThread(thread);
				WinFormTools.MsgBox(ex.ToString());
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result cdHideOther(ExternalCommandData commandData, string selectName, bool isShow)
		{
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			XmlNodeList nodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet[@Text='" + selectName + "']/Cates/Cate");
			ElementSet elementSet = CmdTools.cdGetCateElment(commandData, nodeList, selectName);
			Transaction transaction = new Transaction(activeUIDocument.Document, "www.hongye.com.cn");
			Thread thread = null;
			try
			{
				transaction.Start();
				thread = WinFormTools.ThreadShowProgressBar(commandData);
				if (thread != null)
				{
					thread.Start();
				}
				ElementSet elementSet2 = RevitTools.hyGetAllElement(commandData);
				foreach (object obj in elementSet)
				{
					Element element = (Element)obj;
					elementSet2.Erase(element);
				}
				if (elementSet2.Size == 0)
				{
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				if (isShow)
				{
					application.ActiveUIDocument.ActiveView.UnHideYJKElements(elementSet2);
				}
				else
				{
					application.ActiveUIDocument.ActiveView.HideYJKElements(elementSet2);
				}
				transaction.Commit();
				WinFormTools.DisThread(thread);
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				WinFormTools.DisThread(thread);
				WinFormTools.MsgBox(ex.ToString());
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result FormCreateColl(ExternalCommandData commandData, string selectName)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			string title = activeUIDocument.Document.Title;
			string pathName = activeUIDocument.Document.PathName;
			Result result;
			try
			{
				Selection selection = activeUIDocument.Selection;
				if (!selection.GetElementIds().Any<ElementId>())
				{
					TaskDialog.Show("提示", "请选择元素");
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					XmlDocument xmlDocument = new XmlDocument();
					xmlDocument.LoadXml(XmlInfo.GetSelSet(pathName, title));
					XmlElement documentElement = xmlDocument.DocumentElement;
					XmlElement xmlElement = xmlDocument.CreateElement("Select");
					xmlElement.SetAttribute("Name", selectName);
					xmlElement.SetAttribute("Show", "true");
					xmlElement.SetAttribute("Lock", "false");
					xmlElement.SetAttribute("Check", "false");
					foreach (ElementId elementId in selection.GetElementIds())
					{
						XmlElement xmlElement2 = xmlDocument.CreateElement("ElementID");
						xmlElement2.InnerText = elementId.ToString();
						xmlElement.InsertBefore(xmlElement2, xmlElement.FirstChild);
					}
					documentElement.InsertBefore(xmlElement, documentElement.FirstChild);
					XmlInfo.AddSelSet(pathName, title, xmlDocument.InnerXml);
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch
			{
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public static void cmdLock(ExternalCommandData cmdDataForm, string selName, bool isLock)
		{
			string pathName = cmdDataForm.Application.ActiveUIDocument.Document.PathName;
			string title = cmdDataForm.Application.ActiveUIDocument.Document.Title;
			List<ElementId> list = new List<ElementId>();
			foreach (object obj in XmlInfo.GetSelectSetByName(selName, pathName, title).ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				list.Add(new ElementId(Convert.ToInt32(xmlNode.InnerText)));
			}
			RevitTools.LockElements(cmdDataForm, list, isLock);
		}

		public static void cmdShow(ExternalCommandData cmdDataForm, string selName, bool isShow)
		{
			string pathName = cmdDataForm.Application.ActiveUIDocument.Document.PathName;
			string title = cmdDataForm.Application.ActiveUIDocument.Document.Title;
			List<ElementId> list = new List<ElementId>();
			foreach (object obj in XmlInfo.GetSelectSetByName(selName, pathName, title).ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				list.Add(new ElementId(Convert.ToInt32(xmlNode.InnerText)));
			}
			RevitTools.ShowHideElement(cmdDataForm, list, isShow);
		}

		public static void cmdSelect(ExternalCommandData cmdDataForm, string selName)
		{
			string pathName = cmdDataForm.Application.ActiveUIDocument.Document.PathName;
			string title = cmdDataForm.Application.ActiveUIDocument.Document.Title;
			List<ElementId> list = new List<ElementId>();
			XmlNode selectSetByName = XmlInfo.GetSelectSetByName(selName, pathName, title);
			if (selectSetByName == null)
			{
				return;
			}
			foreach (object obj in selectSetByName.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				list.Add(new ElementId(Convert.ToInt32(xmlNode.InnerText)));
			}
		}

		public static void cmdSelect2(ExternalCommandData cmdDataForm, string selName)
		{
			string pathName = cmdDataForm.Application.ActiveUIDocument.Document.PathName;
			string title = cmdDataForm.Application.ActiveUIDocument.Document.Title;
			List<ElementId> list = new List<ElementId>();
			XmlNode selectSetByName = XmlInfo.GetSelectSetByName(selName, pathName, title);
			if (selectSetByName == null)
			{
				return;
			}
			foreach (object obj in selectSetByName.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				list.Add(new ElementId(Convert.ToInt32(xmlNode.InnerText)));
			}
			RevitTools.SelSelectSet(cmdDataForm, list);
		}

		public static Result FrmAddToColl(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			if (selection.GetElementIds().Count == 0)
			{
				TaskDialog.Show("盈建科提示", "未选择任何元素。");
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			XmlDocument xmlDocument = new XmlDocument();
			string pathName = activeUIDocument.Document.PathName;
			string title = activeUIDocument.Document.Title;
			xmlDocument.LoadXml(XmlInfo.GetSelSet(pathName, title));
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Elements/Select[@Check=\"true\"]");
			if (xmlNodeList.Count == 0)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			foreach (object obj in xmlNodeList)
			{
				XmlNode xmlNode = (XmlNode)obj;
				foreach (ElementId elementId in selection.GetElementIds())
				{
					XmlElement xmlElement = xmlDocument.CreateElement("ElementID");
					xmlElement.InnerText = elementId.ToString();
					xmlNode.AppendChild(xmlElement);
				}
			}
			XmlInfo.AddSelSet(pathName, title, xmlDocument.InnerXml);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result Sel3D(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application application = commandData.Application.Application;
			Transaction transaction = new Transaction(activeUIDocument.Document, "FilterSelect");
			View3D view3D = activeUIDocument.Document.CreatHYView3D(new XYZ(-1.0, 1.0, -1.0));
			try
			{
				transaction.Start();
                Autodesk.Revit.DB.View view = view3D;
				ElementSet allElement = RevitTools.GetAllElement(commandData);
				view.HideYJKElements(allElement);
				allElement.Clear();
				XmlDocument xmlDocument = new XmlDocument();
				string pathName = activeUIDocument.Document.PathName;
				string title = activeUIDocument.Document.Title;
				xmlDocument.LoadXml(XmlInfo.GetSelSet(pathName, title));
				XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Elements/Select");
				bool flag = false;
				foreach (object obj in xmlNodeList)
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Attributes["Check"].Value == "true")
					{
						foreach (object obj2 in xmlNode.ChildNodes)
						{
							XmlNode xmlNode2 = (XmlNode)obj2;
							Element elementById = activeUIDocument.Document.GetElementById(new ElementId(Convert.ToInt32(xmlNode2.InnerText)));
							allElement.Insert(elementById);
						}
						flag = true;
					}
				}
				if (!flag)
				{
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				view.UnHideYJKElements(allElement);
				transaction.Commit();
				activeUIDocument.ActiveView = view3D;
			}
			catch (Exception ex)
			{
				TaskDialog.Show("view", ex.ToString());
				transaction.RollBack();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result FilterSel(ExternalCommandData commandData)
		{
			try
			{
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
                Autodesk.Revit.ApplicationServices.Application application = commandData.Application.Application;
				Transaction transaction = new Transaction(activeUIDocument.Document, "FilterSelect");
				transaction.Start();
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(XmlInfo.xmlCateStr);
				List<string> list = new List<string>();
				foreach (object obj in xmlDocument.SelectNodes("Elements/Cate"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Attributes["Check"].Value == "true")
					{
						list.Add(xmlNode.Attributes["Name"].Value);
					}
				}
				XmlDocument xmlDocument2 = new XmlDocument();
				xmlDocument2.LoadXml(XmlInfo.xmlLevelStr);
				List<string> list2 = new List<string>();
				foreach (object obj2 in xmlDocument2.SelectNodes("Elements/Level"))
				{
					XmlNode xmlNode2 = (XmlNode)obj2;
					if (xmlNode2.Attributes["Check"].Value == "true")
					{
						list2.Add(xmlNode2.Attributes["Name"].Value);
					}
				}
				Selection selection = activeUIDocument.Selection;
				foreach (string b in list)
				{
					if ("风管" == b)
					{
						IEnumerator enumerator = RevitTools.FilterDuct(commandData, list2).GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								object obj3 = enumerator.Current;
								Element element = (Element)obj3;
								selection.GetElementIds().Add(element.Id);
							}
							continue;
						}
					}
					if ("水管" == b)
					{
						IEnumerator enumerator = RevitTools.FilterPipe(commandData, list2).GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								object obj4 = enumerator.Current;
								Element element2 = (Element)obj4;
								selection.GetElementIds().Add(element2.Id);
							}
							continue;
						}
					}
					if ("墙" == b)
					{
						IEnumerator enumerator = RevitTools.FilterWall(commandData, list2).GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								object obj5 = enumerator.Current;
								Element element3 = (Element)obj5;
								selection.GetElementIds().Add(element3.Id);
							}
							continue;
						}
					}
					if ("门" == b)
					{
						IEnumerator enumerator = RevitTools.FilterDoor(commandData, list2).GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								object obj6 = enumerator.Current;
								Element element4 = (Element)obj6;
								selection.GetElementIds().Add(element4.Id);
							}
							continue;
						}
					}
					if ("窗" == b)
					{
						IEnumerator enumerator = RevitTools.FilterWindow(commandData, list2).GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								object obj7 = enumerator.Current;
								Element element5 = (Element)obj7;
								selection.GetElementIds().Add(element5.Id);
							}
							continue;
						}
					}
					if ("风管管件" == b)
					{
						IEnumerator enumerator = RevitTools.FilterFitting(commandData, list2).GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								object obj8 = enumerator.Current;
								Element element6 = (Element)obj8;
								selection.GetElementIds().Add(element6.Id);
							}
							continue;
						}
					}
					if ("风口" == b)
					{
						foreach (object obj9 in RevitTools.FilterDuctTerminal(commandData, list2))
						{
							Element element7 = (Element)obj9;
							selection.GetElementIds().Add(element7.Id);
						}
					}
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result FrmSelSetSelect(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application application = commandData.Application.Application;
			List<ElementId> list = new List<ElementId>();
			Transaction transaction = new Transaction(activeUIDocument.Document, "antiSelect");
			try
			{
				transaction.Start();
				string pathName = activeUIDocument.Document.PathName;
				string title = activeUIDocument.Document.Title;
				foreach (object obj in XmlInfo.GetCheckSelectSet(pathName, title))
				{
					foreach (object obj2 in ((XmlNode)obj).ChildNodes)
					{
						XmlNode xmlNode = (XmlNode)obj2;
						list.Add(new ElementId(Convert.ToInt32(xmlNode.InnerText)));
					}
				}
				bool flag = true;
				if (list.Count > 1000)
				{
                    flag = ((int)TaskDialog.Show("盈建科提示", "要操作的元素数量：" + list.Count + "个，可能会导致死机，是否继续？", (TaskDialogCommonButtons)5) == 1);
				}
				if (flag)
				{
					Selection selection = activeUIDocument.Selection;
					RevitVersionFuncs.ClearSelection(selection);
					List<Element> list2 = new List<Element>();
					foreach (ElementId elementId in list)
					{
						Element elementById = activeUIDocument.Document.GetElementById(elementId);
						list2.Add(elementById);
					}
					RevitVersionFuncs.SetSelection(selection, list2);
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				TaskDialog.Show("ex", ex.ToString());
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result FrmAntiSelect(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application application = commandData.Application.Application;
			List<ElementId> list = new List<ElementId>();
			Transaction transaction = new Transaction(activeUIDocument.Document, "antiSelect");
			try
			{
				transaction.Start();
				XmlDocument xmlDocument = new XmlDocument();
				string pathName = activeUIDocument.Document.PathName;
				string title = activeUIDocument.Document.Title;
				xmlDocument.LoadXml(XmlInfo.GetSelSet(pathName, title));
				foreach (object obj in xmlDocument.SelectNodes("Elements/Select"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Attributes["Check"].Value == "true")
					{
						foreach (object obj2 in xmlNode.ChildNodes)
						{
							XmlNode xmlNode2 = (XmlNode)obj2;
							list.Add(new ElementId(Convert.ToInt32(xmlNode2.InnerText)));
						}
					}
				}
				ElementSet allElement = RevitTools.GetAllElement(commandData);
				bool flag = true;
				if (allElement.Size > 1000)
				{
                    flag = ((int)TaskDialog.Show("盈建科提示", "要操作的元素数量：" + allElement.Size + "个，可能会导致死机，是否继续？", (TaskDialogCommonButtons)5) == 1);
				}
				if (flag)
				{
					Selection selection = activeUIDocument.Selection;
					foreach (ElementId elementId in list)
					{
						Element elementById = activeUIDocument.Document.GetElementById(elementId);
						if (elementById != null)
						{
							allElement.Erase(elementById);
						}
					}
					RevitVersionFuncs.ClearSelection(selection);
					List<Element> list2 = new List<Element>();
					foreach (object obj3 in allElement)
					{
						Element item = (Element)obj3;
						list2.Add(item);
					}
					RevitVersionFuncs.SetSelection(selection, list2);
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				TaskDialog.Show("ex", ex.ToString());
				transaction.RollBack();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static Result FrmMinusColl(ExternalCommandData commandData)
		{
			Result result;
			try
			{
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
                Autodesk.Revit.ApplicationServices.Application application = commandData.Application.Application;
				Selection selection = activeUIDocument.Selection;
				if (selection.GetElementIds().Count == 0)
				{
					TaskDialog.Show("盈建科提示", "未选择任何元素。");
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					XmlDocument xmlDocument = new XmlDocument();
					string pathName = activeUIDocument.Document.PathName;
					string title = activeUIDocument.Document.Title;
					xmlDocument.LoadXml(XmlInfo.GetSelSet(pathName, title));
					XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Elements/Select[@Check=\"true\"]");
					if (xmlNodeList.Count == 0)
					{
						result = Autodesk.Revit.UI.Result.Cancelled;
					}
					else
					{
						foreach (object obj in xmlNodeList)
						{
							foreach (object obj2 in ((XmlNode)obj).ChildNodes)
							{
								XmlNode xmlNode = (XmlNode)obj2;
								foreach (ElementId elementId in selection.GetElementIds())
								{
									if (xmlNode.InnerText == elementId.ToString())
									{
										xmlNode.ParentNode.RemoveChild(xmlNode);
									}
								}
							}
						}
						XmlInfo.AddSelSet(pathName, title, xmlDocument.InnerXml);
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
				}
			}
			catch (Exception ex)
			{
				TaskDialog.Show("minu", ex.ToString());
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

        public static Result FrmMultiSelect(ExternalCommandData commandData, System.Windows.Forms.Form frm)
		{
			UIApplication application = commandData.Application;
			Document document = application.ActiveUIDocument.Document;
			Selection selection = application.ActiveUIDocument.Selection;
			Transaction transaction = new Transaction(document, "http://www.hongye.com.cn");
			transaction.Start();
			new List<Reference>();
			Result result = 0;
			try
			{
				IEnumerable<Reference> enumerable = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, "请从图面选择：");
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(XmlInfo.xmlCountSel);
				XmlNode documentElement = xmlDocument.DocumentElement;
				List<Element> list = new List<Element>();
				foreach (Reference reference in enumerable)
				{
					XmlElement xmlElement = xmlDocument.CreateElement("ElementID");
					xmlElement.InnerText = reference.ElementId.IntegerValue.ToString();
					documentElement.AppendChild(xmlElement);
					Element element = document.GetElement(reference);
					list.Add(element);
				}
				RevitVersionFuncs.SetSelection(selection, list);
				XmlInfo.xmlCountSel = xmlDocument.InnerXml;
				transaction.Commit();
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction.RollBack();
				result = Autodesk.Revit.UI.Result.Failed;
			}
			frm.Show();
			return result;
		}

		public static void CountGetResult(ExternalCommandData commandData)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			XmlDocument xmlDocument = new XmlDocument();
			string pathName = activeUIDocument.Document.PathName;
			string title = activeUIDocument.Document.Title;
			xmlDocument.LoadXml(XmlInfo.GetSelSet(pathName, title));
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Elements/Select");
			XmlElement documentElement = xmlDocument.DocumentElement;
			foreach (object obj in xmlNodeList)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (!(xmlNode.Attributes["Check"].Value == "false"))
				{
					foreach (object obj2 in xmlNode.ChildNodes)
					{
						XmlNode xmlNode2 = (XmlNode)obj2;
						if (!(xmlNode2.InnerText == ""))
						{
							ElementId elementId = new ElementId(int.Parse(xmlNode2.InnerText));
							Pipe pipe = activeUIDocument.Document.GetElementById(elementId) as Pipe;
							if (pipe != null)
							{
								string text = RevitTools.CountGetPipeMater(activeUIDocument.Document, pipe);
								string text2 = RevitTools.getPipeLength(commandData.Application, pipe).ToString();
								string text3 = RevitTools.CountGetPipeSpec(activeUIDocument.Document, pipe);
								if (XmlInfo.CountMaterIsCheck(text) && XmlInfo.CountSpecIsCheck(text3))
								{
									XmlNode xmlNode3 = xmlDocument.SelectSingleNode(string.Concat(new string[]
									{
										"Elements/Select[@Name='",
										xmlNode.Attributes["Name"].Value,
										"']/Mater[@Name='",
										text,
										"']"
									}));
									if (xmlNode3 != null)
									{
										xmlNode3.Attributes["Num"].Value = (int.Parse(xmlNode3.Attributes["Num"].Value) + 1).ToString();
										xmlNode3.Attributes["Length"].Value = (double.Parse(xmlNode3.Attributes["Length"].Value) + double.Parse(text2)).ToString();
										XmlNode xmlNode4 = xmlDocument.SelectSingleNode(string.Concat(new string[]
										{
											"Elements/Select[@Name='",
											xmlNode.Attributes["Name"].Value,
											"']/Mater[@Name='",
											text,
											"']/Spec[@Name='",
											text3,
											"']"
										}));
										if (xmlNode4 != null)
										{
											xmlNode4.Attributes["Num"].Value = (int.Parse(xmlNode4.Attributes["Num"].Value) + 1).ToString();
											xmlNode4.Attributes["Length"].Value = (double.Parse(xmlNode4.Attributes["Length"].Value) + double.Parse(text2)).ToString();
										}
										else
										{
											XmlElement xmlElement = xmlDocument.CreateElement("Spec");
											xmlElement.SetAttribute("Name", text3);
											xmlElement.SetAttribute("Num", "1");
											xmlElement.SetAttribute("Length", text2);
											xmlNode3.AppendChild(xmlElement);
										}
									}
									else
									{
										XmlElement xmlElement2 = xmlDocument.CreateElement("Mater");
										xmlElement2.SetAttribute("Name", text);
										xmlElement2.SetAttribute("Num", "1");
										xmlElement2.SetAttribute("Length", text2);
										XmlElement xmlElement3 = xmlDocument.CreateElement("Spec");
										xmlElement3.SetAttribute("Name", text3);
										xmlElement3.SetAttribute("Num", "1");
										xmlElement3.SetAttribute("Length", text2);
										xmlElement2.AppendChild(xmlElement3);
										xmlNode2.ParentNode.AppendChild(xmlElement2);
									}
								}
							}
						}
					}
				}
			}
			if (activeUIDocument.Selection.GetElementIds().Count > 0)
			{
				XmlElement xmlElement4 = xmlDocument.CreateElement("Select");
				xmlElement4.SetAttribute("Name", "图面选择");
				xmlElement4.SetAttribute("Check", "true");
				documentElement.AppendChild(xmlElement4);
			}
			foreach (ElementId elementId2 in activeUIDocument.Selection.GetElementIds())
			{
				Pipe pipe2 = activeUIDocument.Document.GetElementById(elementId2) as Pipe;
				if (pipe2 != null)
				{
					string text4 = RevitTools.CountGetPipeMater(activeUIDocument.Document, pipe2);
					string text5 = RevitTools.getPipeLength(commandData.Application, pipe2).ToString();
					string text6 = RevitTools.CountGetPipeSpec(activeUIDocument.Document, pipe2);
					if (XmlInfo.CountMaterIsCheck(text4) && XmlInfo.CountSpecIsCheck(text6))
					{
						XmlNode xmlNode5 = xmlDocument.SelectSingleNode("Elements/Select[@Name='图面选择']/Mater[@Name='" + text4 + "']");
						if (xmlNode5 != null)
						{
							xmlNode5.Attributes["Num"].Value = (int.Parse(xmlNode5.Attributes["Num"].Value) + 1).ToString();
							xmlNode5.Attributes["Length"].Value = (double.Parse(xmlNode5.Attributes["Length"].Value) + double.Parse(text5)).ToString();
							XmlNode xmlNode6 = xmlDocument.SelectSingleNode(string.Concat(new string[]
							{
								"Elements/Select[@Name='图面选择']/Mater[@Name='",
								text4,
								"']/Spec[@Name='",
								text6,
								"']"
							}));
							if (xmlNode6 != null)
							{
								xmlNode6.Attributes["Num"].Value = (int.Parse(xmlNode6.Attributes["Num"].Value) + 1).ToString();
								xmlNode6.Attributes["Length"].Value = (double.Parse(xmlNode6.Attributes["Length"].Value) + double.Parse(text5)).ToString();
							}
							else
							{
								XmlElement xmlElement5 = xmlDocument.CreateElement("Spec");
								xmlElement5.SetAttribute("Name", text6);
								xmlElement5.SetAttribute("Num", "1");
								xmlElement5.SetAttribute("Length", text5);
								xmlNode5.AppendChild(xmlElement5);
							}
						}
						else
						{
							XmlElement xmlElement6 = xmlDocument.CreateElement("Mater");
							xmlElement6.SetAttribute("Name", text4);
							xmlElement6.SetAttribute("Num", "1");
							xmlElement6.SetAttribute("Length", text5);
							XmlElement xmlElement7 = xmlDocument.CreateElement("Spec");
							xmlElement7.SetAttribute("Name", text6);
							xmlElement7.SetAttribute("Num", "1");
							xmlElement7.SetAttribute("Length", text5);
							xmlElement6.AppendChild(xmlElement7);
							xmlDocument.SelectSingleNode("Elements/Select[@Name='图面选择']").AppendChild(xmlElement6);
						}
					}
				}
			}
			XmlInfo.xmlCountResult = xmlDocument.InnerXml;
		}

		public static ExternalCommandData cmdDataThread = null;

		public static string selectNameThread = "";

		public static ElementSet elementSetThread = null;
	}
}
